package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.UserFocus;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.mongo.VideoComment;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findMovementsByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page -1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Video.class);
    }

    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").in(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.limit(pagesize).skip((page -1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page,pagesize,count,list);
    }

    //分页查询小视频评论列表
    @Override
    public List<VideoComment> findComments(ObjectId videoId, CommentType commentType, Integer page, Integer pagesize) {
        //1.构造查询条件
        Criteria criteria = Criteria.where("videoId").is(videoId)
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria).skip((page -1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<VideoComment> videoComments = mongoTemplate.find(query, VideoComment.class);
        return videoComments;
    }

    //判断视频用户关注是否关注
    @Override
    public Boolean isFocus(Long userId, Long uid) {
        //构造查询条件
        Query query = Query.query(Criteria.where("userId").is(userId).and("focusUserId").is(uid));
        boolean exists = mongoTemplate.exists(query, UserFocus.class);
        return exists;
    }

    //视频用户关注数据保存
    @Override
    public Long focusUser(UserFocus userFocus) {
        mongoTemplate.save(userFocus);
        //查询该小视频发布人被关注数
        Query query = Query.query(Criteria.where("focusUserId").is(userFocus.getFocusUserId()));
        long count = mongoTemplate.count(query, UserFocus.class);
        return count;
    }

    //视频用户关注数据删除
    @Override
    public Long  unFocusUser(UserFocus userFocus) {
        Query query = Query.query(Criteria.where("userId").is(userFocus.getUserId()).and("focusUserId").is(userFocus.getFocusUserId()));
        mongoTemplate.remove(query,UserFocus.class);
        Query queryCount = Query.query(Criteria.where("focusUserId").is(userFocus.getFocusUserId()));
        long count = mongoTemplate.count(query, UserFocus.class);
        return count;
    }

    /*//修改视频的喜欢，评论，关注数量
    @Override
    public void updateCount(String videoId, Integer commentType,Integer num) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(videoId)));
        //构造修改数据
        Update update = new Update();
        if(commentType == CommentType.COMMENT.getType()){
            update.inc("commentCount",num);
        }else if(commentType == CommentType.LIKE.getType()){
            update.inc("likeCount",num);
        }else {
            update.inc("loveCount",num);
        }
        mongoTemplate.updateFirst(query,update,Video.class);
    }*/
}
